package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.Project;
import com.tongtech.system.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 项目信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    /**
     * 获取项目列表
     */
    @RequiresPermissions("project:info:list")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 根据项目ID获取详细信息
     */
    @RequiresPermissions("project:info:query")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable Long projectId)
    {
        return success(projectService.selectProjectById(projectId));
    }

    /**
     * 根据项目ID获取详细信息
     */
    @RequiresPermissions("project:info:query")
    @GetMapping(value = "/info/{projectId}")
    public AjaxResult getBasicInfo(@PathVariable Long projectId)
    {
        return success(projectService.selectProjectBasicInfoById(projectId));
    }

    /**
     * 新增项目
     */
    @RequiresPermissions("project:info:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Project project)
    {
       if(Objects.nonNull(project)){
           project.setCreateBy(SecurityUtils.getUsername());
       }
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目
     */
    @RequiresPermissions("project:info:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Project project)
    {
        if(Objects.nonNull(project)){
            project.setUpdateBy(SecurityUtils.getUsername());
        }
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("project:info:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectId}")
    public AjaxResult remove(@PathVariable Long projectId)
    {
        return toAjax(projectService.deleteProjectById(projectId));
    }
}
